import FlowGraph from '../../../graph/index'

export function gridOpt(settings) {
  const options = {
    visible: settings.visible,
    type: settings.type,
    args: [
      {
        color: settings.color,
        thickness: settings.thickness
      }
    ]
  }
  if (settings.type === 'doubleMesh') {
    options.args.push({
      color: settings.colorSecond,
      thickness: settings.thicknessSecond,
      factor: settings.factor
    })
  }
  const { graph } = FlowGraph
  graph.drawGrid(options)
}

export function gridSizeOpt(settings) {
  const { graph } = FlowGraph
  graph.setGridSize(settings.size)
}

const tryToJSON = (val) => {
  try {
    return JSON.parse(val)
  } catch (error) {
    return val
  }
}

export function backGroundOpt(settings) {
  const options = {
    color: settings.bgColor,
    image: settings.showImage ? require('@/assets/logo.svg') : undefined,
    repeat: settings.repeat,
    angle: settings.angle,
    size: tryToJSON({ width: settings.bgSize, height: settings.bgSize }),
    position: tryToJSON(settings.position),
    opacity: settings.opacity
  }
  const { graph } = FlowGraph
  graph.drawBackground(options)
}
